package com.library.admin.controller;

import com.library.admin.modules.booktype.entity.BookType;
import com.library.admin.modules.booktype.vo.BookTypeVO;
import com.library.admin.modules.booktype.bo.BookTypeInsert;
import com.library.admin.modules.booktype.bo.BookTypeUpdate;
import com.library.admin.modules.booktype.service.BookTypeService;
import com.library.common.annotation.CurrentUser;
import com.library.common.service.bo.CurrentLoginUser;
import com.library.logging.annotation.LogSys;
import org.springframework.web.bind.annotation.*;
import com.library.common.response.Result;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import java.util.List;
import java.util.Objects;


/**
 * @Author xyh
 * @Desc 图书分类表(BookType)表控制层
 * @Date 2023-10-05 09:46:51
 */

@RestController
@RequestMapping("booktype")
public class BookTypeController {

    @Resource
    private BookTypeService bookTypeService;


    /**
     * 获取全部的分类列表
     *
     * @return 数据
     */
    @LogSys(value = "获取全部的图书分类列表")
    @GetMapping("/list")
    public Result<List<BookType>> getList() {
        return Result.success(bookTypeService.bookTypeList());
    }

    /**
     * 根据ID获取数据
     *
     * @return 单个图书分类表数据
     */
    @LogSys(value = "根据图书分类ID获取数据")
    @PostMapping("/queryById/{id}")
    public Result<BookTypeVO> queryById(@PathVariable("id") Integer id) {
        return Result.success(bookTypeService.queryById(id));
    }


    /**
     * 添加图书分类表
     *
     * @return 新增图书分类表数据
     */
    @LogSys(value = "新增图书分类表数据")
    @PostMapping("/insert")
    public Result insert(@Valid @RequestBody BookTypeInsert param, @CurrentUser CurrentLoginUser currentLoginUser) {
        param.setUsername(currentLoginUser.getUsername());
        bookTypeService.insert(param);
        return Result.success();
    }


    /**
     * 编辑图书分类表
     *
     * @return 编辑图书分类表数据
     */
    @LogSys(value = "编辑图书分类表数据")
    @PutMapping("/update")
    public Result update(@Valid @RequestBody BookTypeUpdate param) {
        if (Objects.isNull(param.getId())) {
            return Result.error("ID不能为空");
        }
        bookTypeService.update(param);
        return Result.success();
    }

    /**
     * 删除图书分类表
     *
     * @return 删除图书分类表数据
     */
    @LogSys(value = "删除图书分类表数据")
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable("id") Integer id) {
        bookTypeService.deleteById(id);
        return Result.success();
    }

    /**
     * 获取图书分类树
     *
     * @return
     */
    @LogSys(value = "获取图书分类树")
    @GetMapping("/tree")
    public Result<List<BookTypeVO>> getBookTypeTree() {
        List<BookTypeVO> bookTypeTree = bookTypeService.getBookTypeTree();
        return Result.success(bookTypeTree);
    }
}
